/*
 * Copyright (c) 2005, 2014 vacoor
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package org.vacoor.nothing.common.dataset.support;

import org.vacoor.nothing.common.dataset.Record;
import org.vacoor.nothing.common.util.Castor;

import java.util.Date;
import java.util.List;

import static org.vacoor.nothing.common.dataset.util.ExcelHelper.columnNameToIndex;

/**
 * Excel 记录行实现
 *
 * @author vacoor
 */
public class SimpleRecord implements Record {
    private final int row;
    private final List<?> rowData;


    public SimpleRecord(int row, List<?> rowData) {
        this.row = row;
        this.rowData = rowData;
    }

    @Override
    public int getRow() {
        return row;
    }

    @Override
    public int getFieldCount() {
        return rowData.size();
    }

    @Override
    public Object getValue(int index) {
        return index < getFieldCount() ? rowData.get(index) : null;
    }

    @Override
    public Object getValue(String name) {
        return getValue(columnNameToIndex(name));
    }

    @Override
    public Boolean getBoolean(int index) {
        return index < getFieldCount() ? Castor.asBoolean(rowData.get(index)) : null;
    }

    @Override
    public Boolean getBoolean(String name) {
        return getBoolean(columnNameToIndex(name));
    }

    @Override
    public Number getNumber(int index) {
        return index < getFieldCount() ? Castor.asNumber(rowData.get(index), Number.class) : null;
    }

    @Override
    public Number getNumber(String name) {
        return getNumber(columnNameToIndex(name));
    }

    @Override
    public String getString(int index) {
        return index < getFieldCount() ? Castor.asString(rowData.get(index)) : null;
    }

    @Override
    public String getString(String name) {
        return getString(columnNameToIndex(name));
    }

    @Override
    public Date getDate(int index) {
        return index < getFieldCount() ? Castor.asDate(rowData.get(index)) : null;
    }

    @Override
    public Date getDate(String name) {
        return getDate(columnNameToIndex(name));
    }
}
